package com.linkhub.oms.dao.mpservice.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.linkhub.oms.common.enums.mp.DataStatusEnum;
import com.linkhub.oms.common.util.QtStreamUtil;
import com.linkhub.oms.dao.mapper.CountryPartitionEntityMapper;
import com.linkhub.oms.dao.mpservice.ICountryPartitionService;
import com.linkhub.oms.entity.CountryPartitionEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 国家分区表 服务实现类
 * </p>
 *
 * @author Frank
 * @since 2024-06-23
 */
@Service
public class CountryPartitionServiceImpl extends ServiceImpl<CountryPartitionEntityMapper, CountryPartitionEntity> implements ICountryPartitionService {

    @Override
    public List<CountryPartitionEntity> listByCountryCode(String countryCode) {
        if (StringUtils.isBlank(countryCode)) {
            return Lists.newArrayList();
        }
        return super.lambdaQuery()
                .eq(CountryPartitionEntity::getCountryCode, countryCode)
                .eq(CountryPartitionEntity::getDataStatus, DataStatusEnum.ENABLE)
                .orderByAsc(CountryPartitionEntity::getId)
                .list();
    }

    @Override
    public Map<String, List<CountryPartitionEntity>> listByCountryCodes(List<String> countryCodes) {
        if (CollUtil.isEmpty(countryCodes)) {
            return MapUtil.empty();
        }
        return QtStreamUtil.group(super.lambdaQuery()
                        .in(CountryPartitionEntity::getCountryCode, countryCodes)
                        .eq(CountryPartitionEntity::getDataStatus, DataStatusEnum.ENABLE)
                        .orderByAsc(CountryPartitionEntity::getId)
                        .list(),
                CountryPartitionEntity::getCountryCode);
    }

    @Override
    public boolean existPartitionOfCountry(String countryCode) {
        if (StringUtils.isBlank(countryCode)) {
            return false;
        }
        return lambdaQuery()
                .eq(CountryPartitionEntity::getCountryCode, countryCode)
                .isNotNull(CountryPartitionEntity::getCountryPartition)
                .eq(CountryPartitionEntity::getDataStatus, DataStatusEnum.ENABLE)
                .exists();
    }
}
